import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;


public class GameFrame {
    static JFrame frame;
    static HashMap<String, ImagePanel> bg_map = new HashMap<String, ImagePanel>();
    static boolean isFirst = true; // 用来标识是否是第一次进入gamePanel
    static boolean islandFirst = true;  //用来识别是否是第一次进入荒岛

    public static void main(String[] args) {
        frame = new JFrame("Robinson Crusoe");
        // there can be icon
        frame.setSize(Settings.frame_w, Settings.frame_h); // need change
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Save.read();

        JPanel panel = new JPanel();
        frame.add(panel);

        initPanel(panel);

        frame.setVisible(true);
    }

    /**
     * 初始化游戏页面
     *
     * @param panel     面板
     */
    private static void initPanel(JPanel panel) {
        System.out.println("New starting panel preparing...");
        panel.removeAll();
        panel.repaint();
        panel.setLayout(null);

        java.net.URL imgURL = GameFrame.class.getResource("imgs/background.png");
        BufferedImage img = null;
        try {
            img = ImageIO.read(new URL(imgURL.toString()));
        } catch(IOException e) {
            e.printStackTrace();
        }
        ImagePanel background_label = new ImagePanel(img);

        java.net.URL buttonURL = GameFrame.class.getResource("imgs/button1.png");
        BufferedImage button1 = null;
        try {
            button1 =ImageIO.read(new URL(buttonURL.toString()));
        } catch(IOException e) {
            e.printStackTrace();
        }

        ImageButton new_game_button = new ImageButton(button1,"New Game");
        ImageButton load_button = new ImageButton(button1,"Load");
        ImageButton settings_button = new ImageButton(button1,"Settings");

        new_game_button.setBorderPainted(false);  // 不绘制边框
        new_game_button.setFocusPainted(false);  // 选中后不绘制边框
        new_game_button.setContentAreaFilled(false);

        load_button.setBorderPainted(false);
        load_button.setFocusPainted(false);
        load_button.setContentAreaFilled(false);

        settings_button.setBorderPainted(false);
        settings_button.setFocusPainted(false);
        settings_button.setContentAreaFilled(false);

        new_game_button.setFont(new java.awt.Font(Settings.button_font, Font.BOLD, Settings.getFont_size()));
        load_button.setFont(new java.awt.Font(Settings.button_font, Font.BOLD, Settings.getFont_size()));
        settings_button.setFont(new java.awt.Font(Settings.button_font, Font.BOLD, Settings.getFont_size()));

        new_game_button.setBounds(Settings.new_game_button_x, Settings.new_game_button_y, Settings.new_game_button_w, Settings.new_game_button_h);
        load_button.setBounds(Settings.new_game_button_x, Settings.load_button_y, Settings.new_game_button_w, Settings.new_game_button_h);
        settings_button.setBounds(Settings.new_game_button_x, Settings.settings_button_y, Settings.new_game_button_w, Settings.new_game_button_h);

        new_game_button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("You choose to start a new game, new Robinson preparing...");
                panel.removeAll();
                panel.repaint();

                Robinson robinson = new Robinson();  // 生成一个全新的Robinson
                Friday friday = new Friday(Settings.choices);

                gamePanel(panel, robinson, friday);
            }
        });

        load_button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("You choose to load a game.");
                panel.removeAll();
                panel.repaint();

                loadPanel(panel , 0);
            }
        });

        settings_button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("You choose to settings.");
                panel.removeAll();
                panel.repaint();
                settingsPanel(panel);
            }
        });

        panel.add(new_game_button);
        panel.add(load_button);
        panel.add(settings_button);
        panel.add(background_label);
    }

    /**
     * 实现玩家操作的游戏面板
     *
     * @param panel     游戏面板
     * @param robinson      鲁滨逊实例,提供数据
     * @param friday        星期五实例,提供数据
     */
    static void gamePanel(JPanel panel, Robinson robinson, Friday friday) {
        panel.removeAll();
        panel.repaint();
        panel.setLayout(null);
        // 显示鲁滨逊的初始状态
        java.net.URL imgURL1 = GameFrame.class.getResource("imgs/gamepanel.jpg");
        BufferedImage img1 = null;
        try {
            img1 = ImageIO.read(new URL(imgURL1.toString()));
        } catch(IOException e) {
            e.printStackTrace();
        }
        ImagePanel gamepanel_label = new ImagePanel(img1);

        java.net.URL imgURL2 = GameFrame.class.getResource("imgs/play.jpg");
        BufferedImage img2 = null;
        try {
            img2 = ImageIO.read(new URL(imgURL2.toString()));
        } catch(IOException e) {
            e.printStackTrace();
        }
        ImagePanel play_label = new ImagePanel(img2);

        java.net.URL imgURL3 = GameFrame.class.getResource("imgs/read.jpg");
        BufferedImage img3 = null;
        try {
            img3 = ImageIO.read(new URL(imgURL3.toString()));
        } catch(IOException e) {
            e.printStackTrace();
        }
        ImagePanel read_label = new ImagePanel(img3);

        java.net.URL imgURL4 = GameFrame.class.getResource("imgs/sail.jpg");
        BufferedImage img4 = null;
        try {
            img4 = ImageIO.read(new URL(imgURL4.toString()));
        } catch(IOException e) {
            e.printStackTrace();
        }
        ImagePanel sail_label = new ImagePanel(img4);

        java.net.URL com_imgURL = GameFrame.class.getResource("imgs/activity.png");
        BufferedImage com_img = null;
        try {
            com_img = ImageIO.read(new URL(com_imgURL.toString()));
        } catch(IOException e) {
            e.printStackTrace();
        }
        ImagePanel common_label = new ImagePanel(com_img);

        BufferedImage hangout_img = null;
        java.net.URL hangout_imgURL = GameFrame.class.getResource("imgs/hangout.png");
        try {
            hangout_img = ImageIO.read(new URL(hangout_imgURL.toString()));
        } catch(IOException e) {
            e.printStackTrace();
        }
        ImagePanel hangout_label = new ImagePanel(hangout_img);

        bg_map.put("game_panel", gamepanel_label);
        bg_map.put("play", play_label);
        bg_map.put("read", read_label);
        bg_map.put("sail", sail_label);
        bg_map.put("common", common_label);
        bg_map.put("hangout", hangout_label);

        if (robinson.background.equals(""))
            robinson.background = "game_panel";

        JLabel age_label = new JLabel("Age: " + robinson.getAge());
        JLabel intelligence_label = new JLabel("Intelligence: " + String.format("%.1f", robinson.getIntelligence()));
        JLabel strength_label = new JLabel("Strength: " + String.format("%.1f", robinson.getStrength()));
        JLabel mood_label = new JLabel("Mood: " +  String.format("%.1f", robinson.getMood()));

        age_label.setBounds(Settings.age_label_x, Settings.age_label_y, Settings.age_label_w, Settings.age_label_h);
        intelligence_label.setBounds(Settings.age_label_x, Settings.intelligence_label_y, Settings.age_label_w, Settings.age_label_h);
        strength_label.setBounds(Settings.age_label_x, Settings.strength_label_y, Settings.age_label_w, Settings.age_label_h);
        mood_label.setBounds(Settings.age_label_x, Settings.mood_label_y, Settings.age_label_w, Settings.age_label_h);

        age_label.setFont(new java.awt.Font(Settings.button_font, Font.BOLD, Settings.getFont_size()));
        intelligence_label.setFont(new java.awt.Font(Settings.button_font, Font.BOLD, Settings.getFont_size()));
        strength_label.setFont(new java.awt.Font(Settings.button_font, Font.BOLD, Settings.getFont_size()));
        mood_label.setFont(new java.awt.Font(Settings.button_font, Font.BOLD, Settings.getFont_size()));

        panel.add(age_label);
        panel.add(intelligence_label);
        panel.add(strength_label);
        panel.add(mood_label);

        JButton save_button = new JButton();
        JButton home_button = new JButton();


        java.net.URL home_imgURL1 = GameFrame.class.getResource("imgs/home_front.png");
        Icon home_front = new ImageIcon(home_imgURL1);
        java.net.URL home_imgURL2 = GameFrame.class.getResource("imgs/home_back.png");
        Icon home_back = new ImageIcon(home_imgURL2);
        java.net.URL save_imgURL1 = GameFrame.class.getResource("imgs/save_front.png");
        Icon save_front = new ImageIcon(save_imgURL1);
        java.net.URL save_imgURL2 = GameFrame.class.getResource("imgs/save_back.png");
        Icon save_back = new ImageIcon(save_imgURL2);

        save_button.setBounds(Settings.save_button_x, Settings.save_button_y, Settings.save_button_w, Settings.save_button_h);
        home_button.setBounds(Settings.home_button_x, Settings.home_button_y, Settings.home_button_w, Settings.home_button_h);

        home_button.setIcon(home_front);
        home_button.setBorderPainted(false);//不绘制边框
        home_button.setFocusPainted(false);//选中后不绘制边框
        home_button.setContentAreaFilled(false);
        home_button.setPressedIcon(home_back);

        save_button.setIcon(save_front);
        save_button.setBorderPainted(false);//不绘制边框
        save_button.setFocusPainted(false);//选中后不绘制边框
        save_button.setContentAreaFilled(false);
        save_button.setPressedIcon(save_back);

        panel.add(save_button);
        panel.add(home_button);

        java.net.URL eat_imgURL = GameFrame.class.getResource("imgs/eat_r.gif");
        java.net.URL read_imgURL = GameFrame.class.getResource("imgs/read_r.gif");
        java.net.URL sail_imgURL = GameFrame.class.getResource("imgs/sail_r.gif");
        java.net.URL play_imgURL = GameFrame.class.getResource("imgs/play_r.gif");
        java.net.URL woodwork_imgURL = GameFrame.class.getResource("imgs/woodwork_r.gif");
        java.net.URL cultivate_imgURL = GameFrame.class.getResource("imgs/cultivate_r.gif");
        java.net.URL sheep_imgURL = GameFrame.class.getResource("imgs/sheep_r.gif");
        java.net.URL hang_out_imgURL = GameFrame.class.getResource("imgs/hangout_r.gif");


        JLabel eat_r = new JLabel(new ImageIcon(eat_imgURL));
        JLabel read_r = new JLabel(new ImageIcon(read_imgURL));
        JLabel sail_r = new JLabel(new ImageIcon(sail_imgURL));
        JLabel play_r = new JLabel(new ImageIcon(play_imgURL));
        JLabel woodwork = new JLabel(new ImageIcon(woodwork_imgURL));
        JLabel cultivate = new JLabel(new ImageIcon(cultivate_imgURL));
        JLabel sheep = new JLabel(new ImageIcon(sheep_imgURL));
        JLabel hangout = new JLabel(new ImageIcon(hang_out_imgURL));

        eat_r.setBounds(0, 0, Settings.frame_w, Settings.frame_h);
        read_r.setBounds(0, 0, Settings.frame_w,Settings.frame_h);
        sail_r.setBounds(0, 0, Settings.frame_w, Settings.frame_h);
        play_r.setBounds(0, 0, Settings.frame_w, Settings.frame_h);
        woodwork.setBounds(0, 0, Settings.frame_w, Settings.frame_h);
        cultivate.setBounds(0, 0, Settings.frame_w, Settings.frame_h);
        sheep.setBounds(0, 0, Settings.frame_w, Settings.frame_h);
        hangout.setBounds(0, 0, Settings.frame_w, Settings.frame_h);

        save_button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("You saved your record.");
                Save.save(robinson, friday);
                SubPanels.tipPanel(panel,bg_map.get(robinson.background), bg_map.get(robinson.background), "Save success!", null,null, 700, friday, robinson.flag);
            }
        });

        home_button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("You go back to starting panel.");
                isFirst = true;
                islandFirst = true;
                Save.save(robinson, friday);
                initPanel(panel);
            }
        });

        if (robinson.sailing_time < 4) { // haven't got onto the island
            java.net.URL at_home_imgURL = GameFrame.class.getResource("imgs/robinson_at_home.png");

            JLabel robinson_label = new JLabel(new ImageIcon(at_home_imgURL));
            robinson_label.setBounds(0,0, Settings.frame_w, Settings.frame_h);

            if (isFirst) {
                panel.add(robinson_label);
                isFirst = false;
            }

            JButton eat_button = new JButton("eat");
            JButton play_button = new JButton("play");
            JButton read_button = new JButton("read");
            JButton sail_button = new JButton("sail");

            eat_button.setBounds(Settings.eat_x, Settings.eat_y, Settings.eat_w, Settings.eat_h);
            play_button.setBounds(Settings.eat_x, Settings.play_y, Settings.eat_w, Settings.eat_h);
            read_button.setBounds(Settings.eat_x,Settings.read_y , Settings.eat_w, Settings.eat_h);
            sail_button.setBounds(Settings.eat_x, Settings.sail_y, Settings.eat_w, Settings.eat_h);

            eat_button.setFocusPainted(false);
            play_button.setFocusPainted(false);
            read_button.setFocusPainted(false);
            sail_button.setFocusPainted(false);

            panel.add(eat_button);
            panel.add(play_button);
            panel.add(read_button);

            if (robinson.getAge() >= 19) {
                panel.add(sail_button);
            }

            sail_button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Robinson sets sail. He feels so happy.");
                    robinson.background = "sail";
                    robinson.sail();
                    robinson.flag ++;
                    update(panel, age_label, intelligence_label, strength_label, mood_label, robinson, friday);
                    if(robinson.sailing_time <= 3)
                         SubPanels.tipPanel(panel, gamepanel_label, bg_map.get(robinson.background), "<html>Strength ++ <br>Intelligence ++ <br>Mood ++</html>", robinson_label, sail_r, 4000, friday, robinson.flag);
                }
            });

            eat_button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Robinson eats dinner. Not so hungry! Yeah!");
                    robinson.background = "game_panel";
                    robinson.eat();
                    robinson.flag++;
                    update(panel, age_label, intelligence_label, strength_label, mood_label, robinson, friday);
                    SubPanels.tipPanel(panel, gamepanel_label, bg_map.get(robinson.background), "Strength ++", robinson_label, eat_r, 2500, friday, robinson.flag);
                }
            });

            play_button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Robinson goes outside to play.");
                    robinson.background = "play";
                    robinson.play();
                    robinson.flag++;
                    update(panel, age_label, intelligence_label, strength_label, mood_label, robinson, friday);
                    SubPanels.tipPanel(panel, gamepanel_label, bg_map.get(robinson.background), "<html>Strength ++ <br>Intelligence ++ <br>Mood ++</html>", robinson_label, play_r, 2500, friday, robinson.flag);
                }
            });

            read_button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Robinson reads books. He gains knowledge.");
                    robinson.background = "read";
                    robinson.read();
                    robinson.flag++;
                    update(panel, age_label, intelligence_label, strength_label, mood_label, robinson, friday);
                    SubPanels.tipPanel(panel, gamepanel_label, bg_map.get(robinson.background), "Intelligence ++", robinson_label, read_r, 2500, friday, robinson.flag);
                }
            });
        }
        else if (robinson.sailing_time == 4) { // onto the island
            if (robinson.background.equals("sail") || islandFirst)
                robinson.background = "common";

            if (robinson.getDate_on_island() == 0) {    // disaster, only one time
                System.out.println("Robinson meets a sea disaster...");
                SubPanels.transitionPanel(panel);
                panel.remove(gamepanel_label);
                System.out.println("The ship wrecked, everyone falls into the sea.");
                // 有一定几率直接去世，体力值越低概率越大
                if (Math.random() * 100 > robinson.getStrength()) {
                    robinson.way_of_death = "drowned in a shipwreck";
                    Save.save(robinson, friday);
                    SubPanels.transitionPanel(panel);
                    System.out.println("Sadly, Robinson is drowned in the shipwreck.");
                    gameOver(panel,robinson.way_of_death,robinson,friday);
                }
                else {
                    SubPanels.disasterFrame();
                    System.out.println("Fortunately, Robinson get onto an island after days of drifting on the sea.");
                    System.out.println("[message] Robinson get onto the island at the age of " + robinson.getAge() + ".");
                    robinson.setDate_on_island(robinson.getAge());  // 记录上岛时间：鲁滨逊的年龄
                    update(panel, age_label, intelligence_label, strength_label, mood_label, robinson, friday);
                }
            }
            else if (robinson.getAge() - robinson.getDate_on_island() == 24 && !robinson.gotFriday){
                System.out.println("A group of unexpected visitors come to the island... A group of savages!");
                if(!SubPanels.saveFridayFrame(robinson))
                {
                    robinson.way_of_death = "caught and roasted to death by savages";
                    System.out.println("Unfortunately, Robinson is caught and roasted to death by these savages.");
                    gameOver(panel, robinson.way_of_death ,robinson, friday);
                }
                else {
                    System.out.println("Luckily, Robinson beats the savages away and saves one person from them. The one that was saved is very grateful, Robinson named him as \"Friday\".");
                }
                update(panel, age_label, intelligence_label, strength_label, mood_label, robinson, friday);
            }
            else if (robinson.getAge() - robinson.getDate_on_island() == 27 && robinson.gotFriday && !robinson.save_father){
                System.out.println("Another group of savages come to the island... Robinson decided to fight with them again!");
                robinson.save_father = true;
                boolean b = SubPanels.saveFather(robinson);
                if(!b)
                {
                    robinson.way_of_death = "killed by savages";
                    System.out.println("Unfortunately, Robinson is killed by these savages.");
                    GameFrame.gameOver(panel, robinson.way_of_death ,robinson, friday);
                }
                else {
                    System.out.println("Luckily, Robinson beats the savages away once again and saves three people from them. Coincidentally, one of the saved people is Friday's father.");
                }
                update(panel, age_label, intelligence_label, strength_label, mood_label, robinson, friday);
            }
            else if (robinson.getAge() - robinson.getDate_on_island() == 28){
                System.out.println("A rebellion broke out on a British ship, and the crew abandoned the captain to the island. Robinson find it a good opportunity to return home, so he together with the captain, he made a plan to take the ship");
                if(SubPanels.leaveFrame(robinson)){
                    robinson.way_of_death = "back home";
                    GameFrame.gameOver(panel, robinson.way_of_death, robinson, friday);
                    System.out.println("backhomie");
                }
                else{
                    robinson.way_of_death = "killed by savages";
                    GameFrame.gameOver(panel, robinson.way_of_death, robinson, friday);
                    System.out.println("28die");
                }
            }

            else {
                java.net.URL wild_imgURL = GameFrame.class.getResource("imgs/wild_r.png");
                JLabel robinson_label = new JLabel(new ImageIcon(wild_imgURL));

                robinson_label.setBounds(0,0,Settings.frame_w, Settings.frame_h);

                JButton woodwork_button = new JButton("woodwork");
                JButton cultivate_button = new JButton("cultivate");
                JButton sheep_button = new JButton("raising sheep");
                JButton hang_out_button = new JButton("hang out");
                JButton do_nothing_button = new JButton("do nothing");

                woodwork_button.setBounds(Settings.wood_x, Settings.wood_y, Settings.wood_w, Settings.wood_h);
                cultivate_button.setBounds(Settings.wood_x, Settings.cultivate_y, Settings.wood_w, Settings.wood_h);
                sheep_button.setBounds(Settings.wood_x, Settings.sheep_y, Settings.wood_w, Settings.wood_h);
                hang_out_button.setBounds(Settings.wood_x, Settings.hang_out_y, Settings.wood_w, Settings.wood_h);
                do_nothing_button.setBounds(Settings.wood_x, Settings.nothing_y, Settings.wood_w, Settings.wood_h);

                woodwork_button.setFocusPainted(false);
                cultivate_button.setFocusPainted(false);
                sheep_button.setFocusPainted(false);
                do_nothing_button.setFocusPainted(false);
                do_nothing_button.setFocusPainted(false);

                panel.add(woodwork_button);
                panel.add(cultivate_button);
                panel.add(sheep_button);
                panel.add(hang_out_button);
                panel.add(do_nothing_button);

                woodwork_button.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        robinson.background = "common";
                        if ( robinson.gotFriday) {
                            SubPanels.arrangeWork(robinson.flag,friday);
                        }
                        robinson.woodwork(friday);
                        robinson.flag++;
                        update(panel, age_label, intelligence_label, strength_label, mood_label, robinson, friday);
                        SubPanels.tipPanel(panel, common_label, bg_map.get(robinson.background), "Woodwork", robinson_label, woodwork, 4000, friday, robinson.flag);
                    }
                });

                cultivate_button.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        robinson.background = "common";
                        if ( robinson.gotFriday) {
                            SubPanels.arrangeWork(robinson.flag,friday);
                        }
                        robinson.cultivate(friday);
                        robinson.flag++;
                        update(panel, age_label, intelligence_label, strength_label, mood_label, robinson, friday);
                        SubPanels.tipPanel(panel, common_label, bg_map.get(robinson.background), "Cultivate", robinson_label, cultivate, 4000, friday, robinson.flag);
                    }
                });

                sheep_button.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        robinson.background = "common";
                        if ( robinson.gotFriday) {
                            SubPanels.arrangeWork(robinson.flag,friday);
                        }
                        robinson.sheep(friday);
                        robinson.flag++;
                        update(panel, age_label, intelligence_label, strength_label, mood_label, robinson, friday);
                        SubPanels.tipPanel(panel, common_label, bg_map.get(robinson.background), "Raising sheep", robinson_label, sheep, 4000, friday, robinson.flag);
                    }
                });

                hang_out_button.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        robinson.background = "hangout";
                        if ( robinson.gotFriday) {
                            SubPanels.arrangeWork(robinson.flag,friday);
                        }
                        robinson.hangout(friday);
                        robinson.flag++;

                        if (robinson.getAge() - robinson.getDate_on_island() == 15 && !robinson.isAlert()) {
                            // 在第15年在岛上逛游发现外岛野人举行人肉宴的痕迹,需要返回给用户信息
                            System.out.println("see");
                            robinson.setAlert();    // 提高警惕的标志
                            System.out.println(robinson.isAlert() + "qwq");
                            System.out.println(panel + "in");
                            SubPanels.traceFrame();
                            System.out.println("trace over");
                            panel.add(robinson_label);
                        }
                        update(panel, age_label, intelligence_label, strength_label, mood_label, robinson, friday);
                        SubPanels.tipPanel(panel, common_label, bg_map.get(robinson.background), "Hangout", robinson_label, hangout, 4000, friday, robinson.flag);
                    }
                });

                do_nothing_button.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        robinson.background = "common";
                        robinson.flag++;
                        update(panel, age_label, intelligence_label, strength_label, mood_label, robinson, friday);
                        SubPanels.tipPanel(panel, common_label, bg_map.get(robinson.background), "Do nothing...", robinson_label, robinson_label, 1000, friday, robinson.flag);
                    }
                });

                if(islandFirst){
                    System.out.println(islandFirst);
                    panel.add(robinson_label);
                    islandFirst = false;
                }

            }
        }
        panel.add(bg_map.get(robinson.background));
    }

    /**
     * 游戏读档页面，可以读取未完成的游戏状态并继续游戏。
     *
     * @param panel 游戏面板
     */
    static void loadPanel(JPanel panel, int page) {
        panel.removeAll();
        panel.repaint();
        panel.setLayout(null);

        int max_page;
        if(Save.index == 0) max_page = 0;
        else if(Save.index % 3 == 0) {
            max_page = Save.index / 3 - 1;
        } else {
            max_page = Save.index / 3;
        }

        panel.setLayout(null);

        java.net.URL load_imgURL = GameFrame.class.getResource("imgs/load.png");
        ImageIcon load_bg_ic = new ImageIcon(load_imgURL);
        JLabel load_bg_lb = new JLabel();
        load_bg_lb.setIcon(load_bg_ic);
        load_bg_lb.setBounds(0, 0, Settings.frame_w, Settings.frame_h);

        String page_information = (page + 1) + "/" + (max_page + 1);
        JLabel page_number = new JLabel(page_information);
        page_number.setBounds(Settings.page_label_x, Settings.page_label_y, Settings.page_label_w, Settings.page_label_h);
        panel.add(page_number);

        java.net.URL record_imgURL = GameFrame.class.getResource("imgs/record_button.png");
        ImageIcon record_icon = new ImageIcon(record_imgURL);

        JButton home_button = new JButton();
        java.net.URL home_imgURL1 = GameFrame.class.getResource("imgs/home_front.png");
        java.net.URL home_imgURL2 = GameFrame.class.getResource("imgs/home_back.png");
        Icon home_front = new ImageIcon(home_imgURL1);
        Icon home_back = new ImageIcon(home_imgURL2);
        home_button.setBounds(Settings.home_button_x_for_load, Settings.home_button_y_for_load, Settings.home_button_w, Settings.home_button_h);
        home_button.setIcon(home_front);
        home_button.setBorderPainted(false);  // 不绘制边框
        home_button.setFocusPainted(false);  // 选中后不绘制边框
        home_button.setContentAreaFilled(false);
        home_button.setPressedIcon(home_back);
        panel.add(home_button);

        JButton left_button = new JButton();
        java.net.URL left_imgURL1 = GameFrame.class.getResource("imgs/left_front.png");
        java.net.URL left_imgURL2 = GameFrame.class.getResource("imgs/left_back.png");
        Icon left_front = new ImageIcon(left_imgURL1);
        Icon left_back = new ImageIcon(left_imgURL2);
        left_button.setBounds(Settings.left_button_x, Settings.left_button_y, Settings.left_button_w, Settings.left_button_h);
        left_button.setIcon(left_front);
        left_button.setBorderPainted(false);  // 不绘制边框
        left_button.setFocusPainted(false);  // 选中后不绘制边框
        left_button.setContentAreaFilled(false);
        left_button.setPressedIcon(left_back);
        panel.add(left_button);

        JButton right_button = new JButton();
        java.net.URL right_imgURL1 = GameFrame.class.getResource("imgs/right_front.png");
        java.net.URL right_imgURL2 = GameFrame.class.getResource("imgs/right_back.png");
        Icon right_front = new ImageIcon(right_imgURL1);
        Icon right_back = new ImageIcon(right_imgURL2);
        right_button.setBounds(Settings.right_button_x, Settings.right_button_y, Settings.right_button_w, Settings.right_button_h);
        right_button.setIcon(right_front);
        right_button.setBorderPainted(false);
        right_button.setFocusPainted(false);
        right_button.setContentAreaFilled(false);
        right_button.setPressedIcon(right_back);
        panel.add(right_button);

        if(page * 3 < Save.robinsonList.size()) {
            JButton delete_button = new JButton();
            java.net.URL delete_imgURL1 = GameFrame.class.getResource("imgs/delete_button.png");
            Icon delete_icon = new ImageIcon(delete_imgURL1);
            delete_button.setBounds(Settings.delete_button_x, Settings.delete_button_y_1, Settings.delete_button_w, Settings.delete_button_h);
            delete_button.setIcon(delete_icon);
            delete_button.setBorderPainted(false);
            delete_button.setFocusPainted(false);
            delete_button.setContentAreaFilled(false);
            panel.add(delete_button);

            delete_button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Save.deleteRecord(page * 3);
                    loadPanel(panel, 0);
                }
            });

            String line1 = "saved time : " + Save.robinsonList.get(page * 3).getTime();
            String line2 = "age : " + Save.robinsonList.get(page * 3).getAge() + "mood : " + String.format("%.1f", Save.robinsonList.get(page * 3).getMood()) +
                    "strength : " + String.format("%.1f", Save.robinsonList.get(page * 3).getStrength()) + "intelligence : " + String.format("%.1f", Save.robinsonList.get(page * 3).getIntelligence());
            String line3 = "sailing time : " + Save.robinsonList.get(page * 3).getSailing_time();
            String information = "<html>" + line1 + "<br>" + line2 + "<br>" + line3 + "</html>";
            JButton button1 = new JButton(information);
            button1.setFont(new java.awt.Font(Settings.button_font, Font.BOLD, Settings.getFont_size()));
            button1.setBounds(Settings.record_button_x, Settings.record_button_y_1, Settings.record_button_w, Settings.record_button_h);
            button1.setFocusPainted(false);
            button1.setBackground(new Color(0x0000000, true));
            button1.setContentAreaFilled(false);
            button1.setOpaque(false);
            button1.setBorderPainted(false);
            panel.add(button1);


            JLabel record = new JLabel();
            record.setIcon(record_icon);
            record.setBounds(Settings.record_button_x, Settings.record_button_y_1, Settings.record_button_w, Settings.record_button_h);
            panel.add(record);

            button1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    gamePanel(panel, Save.robinsonList.get(page * 3), Save.fridayList.get(page * 3));
                }
            });
        }

        if(page * 3 + 1 < Save.robinsonList.size()) {
            JButton delete_button = new JButton();
            java.net.URL delete_imgURL1 = GameFrame.class.getResource("imgs/delete_button.png");
            Icon delete_icon = new ImageIcon(delete_imgURL1);
            delete_button.setBounds(Settings.delete_button_x, Settings.delete_button_y_2, Settings.delete_button_w, Settings.delete_button_h);
            delete_button.setIcon(delete_icon);
            delete_button.setBorderPainted(false);//不绘制边框
            delete_button.setFocusPainted(false);//选中后不绘制边框
            delete_button.setContentAreaFilled(false);
            panel.add(delete_button);

            delete_button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Save.deleteRecord(page * 3 + 1);
                    loadPanel(panel, 0);
                }
            });

            String line1 = "saved time : " + Save.robinsonList.get(page * 3 + 1).getTime();
            String line2 = "age : " + Save.robinsonList.get(page * 3 + 1).getAge() + "mood : " + String.format("%.1f", Save.robinsonList.get(page * 3).getMood()) +
                    "strength : " + String.format("%.1f", Save.robinsonList.get(page * 3 + 1).getStrength()) + "intelligence : " + String.format("%.1f", Save.robinsonList.get(page * 3).getIntelligence());
            String line3 = "sailing time : " + Save.robinsonList.get(page * 3 + 1).getSailing_time();
            String information = "<html>" + line1 + "<br>" + line2+ "<br>" + line3 + "</html>";
            JButton button2 = new JButton(information);
            button2.setFont(new java.awt.Font(Settings.button_font, Font.BOLD, Settings.getFont_size()));
            button2.setBounds(Settings.record_button_x, Settings.record_button_y_2, Settings.record_button_w, Settings.record_button_h);
            button2.setFocusPainted(false);
            button2.setBackground(new Color(0x0000000, true));
            button2.setOpaque(false);
            button2.setBorderPainted(false);
            panel.add(button2);

            JLabel record = new JLabel();
            record.setIcon(record_icon);
            record.setBounds(Settings.record_button_x, Settings.record_button_y_2, Settings.record_button_w, Settings.record_button_h);
            panel.add(record);

            button2.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    gamePanel(panel, Save.robinsonList.get(page * 3 + 1), Save.fridayList.get(page * 3 + 1));
                }
            });
        }

        if(page * 3 + 2 < Save.robinsonList.size()) {
            JButton delete_button = new JButton();
            java.net.URL delete_imgURL1 = GameFrame.class.getResource("imgs/delete_button.png");
            Icon delete_icon = new ImageIcon(delete_imgURL1);
            delete_button.setBounds(Settings.delete_button_x, Settings.delete_button_y_3, Settings.delete_button_w, Settings.delete_button_h);
            delete_button.setIcon(delete_icon);
            delete_button.setBorderPainted(false);//不绘制边框
            delete_button.setFocusPainted(false);//选中后不绘制边框
            delete_button.setContentAreaFilled(false);
            panel.add(delete_button);

            delete_button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Save.deleteRecord(page * 3 + 2);
                    loadPanel(panel, 0);
                }
            });

            String line1 = "saved time : " + Save.robinsonList.get(page * 3 + 2).getTime();
            String line2 = "age : " + Save.robinsonList.get(page * 3 + 2).getAge() + "mood : " + String.format("%.1f", Save.robinsonList.get(page * 3 + 2).getMood()) +
                    "strength : " + String.format("%.1f", Save.robinsonList.get(page * 3 + 2).getStrength()) + "intelligence : " + String.format("%.1f", Save.robinsonList.get(page * 3).getIntelligence());
            String line3 = "sailing time : " + Save.robinsonList.get(page * 3 + 2).getSailing_time();
            String information = "<html>" + line1 + "<br>" + line2+ "<br>" + line3 + "</html>";
            JButton button3 = new JButton(information);
            button3.setFont(new java.awt.Font(Settings.button_font, Font.BOLD, Settings.getFont_size()));
            button3.setBounds(Settings.record_button_x, Settings.record_button_y_3, Settings.record_button_w, Settings.record_button_h);
            button3.setFocusPainted(false);
            button3.setBackground(new Color(0x0000000, true));
            button3.setOpaque(false);
            button3.setBorderPainted(false);
            panel.add(button3);

            JLabel record = new JLabel();
            record.setIcon(record_icon);
            record.setBounds(Settings.record_button_x, Settings.record_button_y_3, Settings.record_button_w, Settings.record_button_h);
            panel.add(record);

            button3.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    gamePanel(panel, Save.robinsonList.get(page * 3 + 2), Save.fridayList.get(page * 3 + 2));
                }
            });
        }


        panel.add(load_bg_lb);

        home_button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                initPanel(panel);
            }
        });

        left_button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(page > 0)
                    loadPanel(panel, page - 1);
            }
        });

        right_button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(page < max_page)
                    loadPanel(panel, page + 1);

            }
        });
    }
    /**
     * 游戏设置页面，可以设置游戏背景音的开关(未实现)、字体的大小
     *
     * @param panel 游戏面板
     */
    static void settingsPanel(JPanel panel) {
        panel.setLayout(null);
        // set the background
        java.net.URL settings_imgURL = GameFrame.class.getResource("imgs/settings_bg.jpg");
        ImageIcon settings_bg_ic = new ImageIcon(settings_imgURL);
        JLabel settings_bg_lb = new JLabel();
        settings_bg_lb.setIcon(settings_bg_ic);
        settings_bg_lb.setBounds(0, 0, Settings.frame_w, Settings.frame_h);

        JLabel font_size_label = new JLabel("Font Size       " + Settings.getFont_size());
        font_size_label.setBounds(Settings.ft_sz_lb_x, Settings.ft_sz_lb_y, Settings.ft_sz_lb_w, Settings.ft_sz_lb_h);
        font_size_label.setFont(new java.awt.Font(Settings.button_font, Font.BOLD, Settings.getFont_size() + 5));
        panel.add(font_size_label);

        java.net.URL add_imgURL1 = GameFrame.class.getResource("imgs/add_front.png");
        java.net.URL add_imgURL2 = GameFrame.class.getResource("imgs/add_back.png");
        Icon add_front = new ImageIcon(add_imgURL1);
        Icon add_back = new ImageIcon(add_imgURL2);
        JButton add_font_size_button = new JButton();
        add_font_size_button.setIcon(add_front);
        add_font_size_button.setPressedIcon(add_back);
        add_font_size_button.setFocusPainted(false);
        add_font_size_button.setBorderPainted(false);
        add_font_size_button.setContentAreaFilled(false);

        java.net.URL minus_imgURL1 = GameFrame.class.getResource("imgs/minus_front.png");
        java.net.URL minus_imgURL2 = GameFrame.class.getResource("imgs/minus_back.png");
        Icon minus_front = new ImageIcon(minus_imgURL1);
        Icon minus_back = new ImageIcon(minus_imgURL2);
        JButton minus_font_size_button = new JButton();
        minus_font_size_button.setIcon(minus_front);
        minus_font_size_button.setPressedIcon(minus_back);
        minus_font_size_button.setFocusPainted(false);
        minus_font_size_button.setBorderPainted(false);
        minus_font_size_button.setContentAreaFilled(false);
        minus_font_size_button.setVisible(true);

        JButton help_button = new JButton();
        JButton home_button = new JButton();

        java.net.URL home_imgURL1 = GameFrame.class.getResource("imgs/home_front.png");
        java.net.URL home_imgURL2 = GameFrame.class.getResource("imgs/home_back.png");
        java.net.URL help_imgURL = GameFrame.class.getResource("imgs/help_button.png");
        Icon home_front = new ImageIcon(home_imgURL1);
        Icon home_back = new ImageIcon(home_imgURL2);
        Icon help = new ImageIcon(help_imgURL);

        help_button.setBounds(Settings.save_button_x - 50 , Settings.save_button_y , Settings.save_button_w, Settings.save_button_h);
        home_button.setBounds(Settings.home_button_x - 50 , Settings.home_button_y , Settings.home_button_w, Settings.home_button_h);

        home_button.setIcon(home_front);
        home_button.setBorderPainted(false);
        home_button.setFocusPainted(false);
        home_button.setContentAreaFilled(false);
        home_button.setPressedIcon(home_back);

        help_button.setIcon(help);
        help_button.setBorderPainted(false);
        help_button.setFocusPainted(false);
        help_button.setContentAreaFilled(false);

        add_font_size_button.setBounds(Settings.add_ft_sz_bt_x - 40, Settings.ft_sz_lb_y - 20, 71, 71);
        minus_font_size_button.setBounds(Settings.minus_ft_sz_bt_x - 30, Settings.ft_sz_lb_y - 20, 71, 71);


        panel.add(add_font_size_button);
        panel.add(minus_font_size_button);
        panel.add(help_button);
        panel.add(home_button);
        panel.add(settings_bg_lb);

        add_font_size_button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Settings.setFont_size(Settings.getFont_size() + 1);
                font_size_label.setText("Font Size       " + Settings.getFont_size());
                panel.repaint();
            }
        });

        minus_font_size_button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Settings.setFont_size(Settings.getFont_size() - 1);
                font_size_label.setText("Font Size       " + Settings.getFont_size());
                panel.repaint();
            }
        });


        help_button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                BufferedImage help= null;
                java.net.URL help_imgURL = GameFrame.class.getResource("imgs/help.jpg");
                try{
                    help = ImageIO.read(new URL(help_imgURL.toString()));
                }catch(IOException ei){
                    ei.printStackTrace();
                }
                ImagePanel help_label=new ImagePanel(help);
                SubPanels.helpPanel(help_label);
            }
        });

        home_button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                initPanel(panel);
            }
        });
    }
    /**
     * 游戏结束页面
     *
     * @param panel     显示游戏状态的面板
     * @param robinson  鲁滨逊实例，提供数据
     * @param friday    星期五实例，提供数据
     */
    static void gameOver(JPanel panel, String text, Robinson robinson, Friday friday) {
        panel.removeAll();
        panel.repaint();
        panel.setLayout(null);

        BufferedImage home_weak = null;
        java.net.URL home_week_imgURL = GameFrame.class.getResource("imgs/home_weak_death.png");
        try {
            home_weak = ImageIO.read(new URL(home_week_imgURL.toString()));
        } catch(IOException e) {
            e.printStackTrace();
        }
        ImagePanel home_weak_death = new ImagePanel(home_weak);            //心情过低/体力太差

        BufferedImage island_weak = null;
        java.net.URL island_week_imgURL = GameFrame.class.getResource("imgs/island_weak_death.png");
        try {
            island_weak = ImageIO.read(new URL(island_week_imgURL.toString()));
        } catch(IOException e) {
            e.printStackTrace();
        }
        ImagePanel island_weak_death = new ImagePanel(island_weak);             //心情过低/体力太差

        BufferedImage drowned = null;
        java.net.URL drowned_imgURL = GameFrame.class.getResource("imgs/drowned_death.png");
        try {
            drowned = ImageIO.read(new URL(drowned_imgURL.toString()));
        } catch(IOException e) {
            e.printStackTrace();
        }
        ImagePanel drowned_death = new ImagePanel(drowned);            //海难

        BufferedImage nature = null;
        java.net.URL nature_imgURL = GameFrame.class.getResource("imgs/nature_death.png");
        try {
            nature = ImageIO.read(new URL(nature_imgURL.toString()));
        } catch(IOException e) {
            e.printStackTrace();
        }
        ImagePanel nature_death = new ImagePanel(nature);                 //自然死亡 老死

        BufferedImage roasted = null;
        java.net.URL roasted_imgURL = GameFrame.class.getResource("imgs/roasted_death.png");
        try {
            roasted = ImageIO.read(new URL(roasted_imgURL.toString()));
        } catch(IOException e) {
            e.printStackTrace();
        }
        ImagePanel roasted_death = new ImagePanel(roasted);              //24年野人

        BufferedImage killed = null;
        java.net.URL killed_imgURL = GameFrame.class.getResource("imgs/killed_death.png");
        try {
            killed = ImageIO.read(new URL(killed_imgURL.toString()));
        } catch(IOException e) {
            e.printStackTrace();
        }
        ImagePanel killed_death = new ImagePanel(killed);            //27、28年野人

        BufferedImage back = null;
        java.net.URL back_imgURL = GameFrame.class.getResource("imgs/backhome.png");
        try {
            back = ImageIO.read(new URL(back_imgURL.toString()));
        } catch(IOException e) {
            e.printStackTrace();
        }
        ImagePanel back_living = new ImagePanel(back);            //27、28年野人

        bg_map.put("too weak or in a bad mood", home_weak_death);
        bg_map.put("in a bad mood or too weak", island_weak_death);
        bg_map.put("drowned in a shipwreck", drowned_death);
        bg_map.put("die of old age", nature_death);
        bg_map.put("caught and roasted to death by savages", roasted_death);
        bg_map.put("killed by savages", killed_death);
        bg_map.put("back home", back_living);

        String information;

        if (text.equals("back home")) {
            information = "<html>" + text + "<br>Age: " + robinson.getAge() + "<br>Intelligence: " + String.format("%.1f", robinson.getIntelligence()) +
                    "<br>Strength: " + String.format("%.1f", robinson.getStrength()) + "<br>Mood: " + String.format("%.1f", robinson.getMood()) + "</html>";
        }
        else{
            information = "<html>Cause of death: " + text + "<br>Age: " + robinson.getAge() + "<br>Intelligence: " + String.format("%.1f", robinson.getIntelligence()) +
                    "<br>Strength: " + String.format("%.1f", robinson.getStrength()) + "<br>Mood: " + String.format("%.1f", robinson.getMood()) + "</html>";
        }
        JLabel message = new JLabel(information);
        message.setBounds(500,280,200,300);
        message.setFont(new java.awt.Font(Settings.button_font, Font.BOLD, Settings.getFont_size()));
        panel.add(message);

        System.out.println("si");

        JButton home_button = new JButton();
        java.net.URL home_imgURL1 = GameFrame.class.getResource("imgs/home_front.png");
        java.net.URL home_imgURL2 = GameFrame.class.getResource("imgs/home_back.png");
        Icon home_front = new ImageIcon(home_imgURL1);
        Icon home_back = new ImageIcon(home_imgURL2);

        home_button.setBounds(Settings.home_button_x, Settings.home_button_y+100, Settings.home_button_w, Settings.home_button_h);
        home_button.setIcon(home_front);
        home_button.setBorderPainted(false);
        home_button.setFocusPainted(false);
        home_button.setContentAreaFilled(false);
        home_button.setPressedIcon(home_back);


        panel.add(home_button);
        if (!"".equals(text)){
            ImagePanel background = bg_map.get(text);
            panel.add(background);
        }
        home_button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                isFirst = true;
                islandFirst = true;
                Save.save(robinson, friday);
                initPanel(panel);
            }
        });
        System.out.println("si end");
    }

    /**
     * 在用户每做出一次选择后更新鲁滨逊（和星期五）的状态并将部分参数实时更新至游戏面板
     *
     * @param panel                 显示游戏状态的面板
     * @param age_label             显示更新鲁滨逊的年龄
     * @param intelligence_label    显示更新鲁滨逊的智力
     * @param strength_label        显示更新鲁滨逊的体力
     * @param mood_label            显示更新鲁滨逊的心情
     * @param robinson              鲁滨逊实例，为更新提供数据
     * @param friday                星期五实例，为更新提供数据
     */
    private static void update(JPanel panel, JLabel age_label, JLabel intelligence_label, JLabel strength_label, JLabel mood_label,  Robinson robinson, Friday friday){
        robinson.updateHealth(robinson.flag, 4);
        // update robinson's status
        age_label.setText("Age: " + robinson.getAge());
        intelligence_label.setText("Intelligence: " +  String.format("%.1f", robinson.getIntelligence()));
        strength_label.setText("Strength: " + String.format("%.1f", robinson.getStrength()));
        mood_label.setText("Mood: " + String.format("%.1f", robinson.getMood()));
        panel.repaint();

        if (!robinson.isLive()){
            if (robinson.getAge() >= 50){
                robinson.way_of_death = "die of old age";
                gameOver(panel, robinson.way_of_death, robinson, friday);
                System.out.println("ooooold");
            }
            else if (robinson.getMood() < 1 || robinson.getStrength() < 1){
                if(robinson.getDate_on_island() == 0){
                    robinson.way_of_death = "too weak or in a bad mood";
                    gameOver(panel, robinson.way_of_death, robinson, friday);
                    System.out.println("saaaadhomie");
                }
                else{
                    robinson.way_of_death = "in a bad mood or too weak";
                    gameOver(panel, robinson.way_of_death, robinson, friday);
                    System.out.println("islandsad");
                }
            }
        }
        else {
            if (robinson.flag == 4){
                robinson.flag = 0;
            }
            gamePanel(panel, robinson, friday);
        }
    }
}
